package core
{
	import flash.display.Sprite;

	public class Node extends GameData
	{
		private var _col : int;
		private var _row : int;
		/**
		 * 类型：梯子，地板，空气
		 */
		public var type : int;
		/**
		 * 地板强度
		 * 可承受几次攻击破碎
		 */
		public var groundHardness : int = 3;
		/**
		 * 地板的资源名称
		 */
		public var groundRes : MapRes = new MapRes();
		/**
		 * 梯子的资源名称
		 */
		public var ladderRes : MapRes = new MapRes();

		/**
		 * 其他资源形态
		 */
		public var res : Array = [];
		/**
		 * 地图其他元素
		 */
		public var otherData : Array = [];

		private var _tileContainer : Sprite;

		/**
		 * 地表容器
		 */
		public function get tileContainer() : Sprite
		{
			return _tileContainer;
		}

		/**
		 * @private
		 */
		public function set tileContainer(value : Sprite) : void
		{
			_tileContainer = value;
		}

		private var _ladderContainer : Sprite;

		/**
		 * 楼梯层
		 */
		public function get ladderContainer() : Sprite
		{
			return _ladderContainer;
		}

		/**
		 * @private
		 */
		public function set ladderContainer(value : Sprite) : void
		{
			_ladderContainer = value;
		}

		public function toString() : String
		{
			var str : String = "";
			str += type + "," + groundHardness + "," + col + "," + col;

			if(groundRes && groundRes.resName)
			{
				str += ",ground:" + groundRes.toString();
			}

			if(ladderRes && ladderRes.resName)
			{
				str += ",ladder:" + ladderRes.toString();
			}
			str += ",res:";

			for each(var mapRes : MapRes in res)
			{
				str += mapRes.toString() + ",";
			}
			return str;
		}

		public function Node()
		{

		}

		public function reset() : void
		{
			for each(var mapRes : MapRes in otherData)
			{
				mapRes.remove();
			}
			otherData.length = 0;
			res.length = 0;

			type = 0;
			groundHardness = 0;
			groundRes = null;
			ladderRes = null;

			clearElements();
		}

		public function clearElements() : void
		{
			while(tileContainer.numChildren > 0)
			{
				tileContainer.removeChildAt(0);
			}

			while(ladderContainer.numChildren > 0)
			{
				ladderContainer.removeChildAt(0);
			}
		}


		override public function checkData() : void
		{
			var obj : Object
			var mapRes : MapRes;
			var mapElement : MapElement;
			var newRes : Array = [];
			var newOtherRes : Array = [];

			for each(obj in res)
			{
				mapRes = new MapRes();
				Config.readObject(mapRes, obj);
				newRes.push(mapRes);
			}

			for each(obj in otherData)
			{
				mapElement = new MapElement();
				Config.readObject(mapElement, obj);
				newOtherRes.push(mapElement);
				mapElement.start_row = row;
				mapElement.start_col = col;
			}
			res = newRes;
			otherData = newOtherRes;
		}

		public function get col() : int
		{
			return _col;
		}

		public function set col(value : int) : void
		{
			_col = value;
		}

		public function get row() : int
		{
			return _row;
		}

		public function set row(value : int) : void
		{
			_row = value;
		}


	}
}